# -*- coding: utf-8 -*-
class EmployeeController < ApplicationController
  before_filter :employee_authorize
  before_filter :sys_before_log
  before_filter :employee_action_hx
  before_filter :privilege_filter, :except => [:authLogin, :validate_auth]

  #用户，员工管理主页面
  def employeeList

  end

  #显示添加页面
  def newEmployee

  end

  #显示编辑页面
  def editEmployee

  end

  def getAllEmployees
    forCBB = params[:for_cbb]
    if forCBB != 'true'
      forCBB = 'false'
    end
    start = params[:start]
    limit = params[:limit]
    if params[:query].nil?
      queryStr = ""
    else
      queryStr = params[:query]
    end

    role = Role.find(session[:role_id])
    if role.cross_company == "否"
      employees = Employee.name_like queryStr, forCBB, session[:company_id]
    else
      employees = Employee.name_like queryStr, forCBB, nil
    end


    if start.nil?
      employeesNow = employees
    else
      employeesNow = employees.paginate start, limit
    end


    if forCBB == 'true'
      jsonStr="{totalProperty:"+employees.length.to_s+",root:"+employeesNow.to_json()+"}"
    else
      jsonStr="{totalProperty:"+employees.length.to_s+",root:"+employeesNow.to_json(:include => {:company=>{:only=>[:id,:name]},:department=>{:only=>[:id,:name]},:role=>{:only=>[:id,:name]}})+"}"
    end
    #logger.info jsonStr
    render :text => jsonStr, :layout => false
  end

  #锁定或解锁用户
  def employeeLock
    employee = Employee.find(params[:id])
    if employee.is_locked == 1
      employee.is_locked = 0
    else
      employee.is_locked = 1
    end
    employee.save
    info = "成功"
    result = "{success:true,info:'#{info}'}"
    render :text => result, :layout => false
  end

  def create
    begin
      employee = Employee.new()
      employee.company_id = params[:company_id]
      employee.number = params[:number]
      employee.login_id = params[:login_id]
      employee.name = params[:name]
      employee.password = params[:password]
      employee.auth_password = params[:auth_password]
      employee.identity_card = params[:identity_card]
      employee.phone = params[:phone]
      employee.email = params[:email]
      employee.address = params[:address]
      employee.department_id = params[:department_id]
      employee.role_id = params[:role_id]
      employee.entry_date = params[:entry_date]
      employee.quit_date = params[:quit_date]
      employee.quit_job = params[:quit_job]
      employee.ab = params[:ab]
      employee.description = params[:description]
      employee.save!
      info = "成功"
    rescue Exception => e
      info = e
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip, e.to_s)
    end
    result = "{success:true,info:'#{info}'}"
    render :text => result, :layout => false
  end

  #编辑前的数据准备
  def edit
    employee = Employee.find(params[:id])
    rootStr = employee.to_json()
    jsonStr = "{root:["+rootStr+"]}"
    logger.debug jsonStr
    render :text => jsonStr, :layout => false
  end

  #把编辑后的数据，更新到数据库中
  def update
    logger.info params[:authorization_password]
    begin
      employee = Employee.find(params[:id])
      employee.number = params[:number]
      employee.company_id = params[:company_id]
      employee.login_id = params[:login_id]
      employee.name = params[:name]
      employee.password = params[:password]
      employee.auth_password = params[:auth_password]
      employee.identity_card = params[:identity_card]
      employee.phone = params[:phone]
      employee.email = params[:email]
      employee.address = params[:address]
      employee.department_id = params[:department_id]
      employee.role_id = params[:role_id]
      employee.entry_date = params[:entry_date]
      employee.quit_date = params[:quit_date]
      employee.quit_job = params[:quit_job]
      employee.ab = params[:ab]
      employee.description = params[:description]
      employee.save!
      info = "成功"
    rescue Exception => e
      info = e.to_s
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip, e.to_s)
    end
    result = "{success:true,info:'#{info}'}"
    render :text => result, :layout => false
  end

  #授信登录
  def validate_auth
    begin
      loginId = params[:login_id]
      authPassword = params[:auth_password]
      companyId = session[:company_id]
      employee = Employee.where("company_id = ? AND login_id = ? AND auth_password = ? AND is_locked <> 1", companyId, loginId, authPassword).first

      discount = 1
      if employee == nil
        #登录失败也记录一下。        
        LoginLog.recordLoginInfo(nil, nil, nil, loginId, authPassword, request.remote_ip, nil)
        info="授权登录失败!"
      else
        discount = employee.role.discount
        LoginLog.recordLoginInfo(session[:company_id], session[:department_id], session[:employee_id], loginId, authPassword, request.remote_ip, nil)
        info = "成功"
      end

      result = "{success:true,info:'#{info}',discount:#{discount}}"
      logger.info result
      render :text => result, :layout => false
    rescue Exception => e
      logger.info "----------------------Exception--------------------------"
      logger.info e
    end
  end
end
